package com.szqhyxj.youxiangju.biz.equipmentActivation;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.google.zxing.integration.android.IntentIntegrator;
import com.google.zxing.integration.android.IntentResult;
import com.szqhyxj.youxiangju.BaseActivity;
import com.szqhyxj.youxiangju.R;
import com.szqhyxj.youxiangju.biz.equipmentActivation.activationSuccess.ActivationSuccessActivity;
import com.szqhyxj.youxiangju.biz.merchantHome.shop.pay.PayState;
import com.szqhyxj.youxiangju.biz.zxing.ScanActivity;
import com.szqhyxj.youxiangju.sys.components.NavigationBar;
import com.szqhyxj.youxiangju.sys.components.androidpicker.AddressPickTask;
import com.szqhyxj.youxiangju.sys.components.androidpicker.qqtheme.framework.entity.City;
import com.szqhyxj.youxiangju.sys.components.androidpicker.qqtheme.framework.entity.County;
import com.szqhyxj.youxiangju.sys.components.androidpicker.qqtheme.framework.entity.Province;
import com.szqhyxj.youxiangju.sys.components.androidpicker.qqtheme.framework.util.ConvertUtils;
import com.szqhyxj.youxiangju.sys.components.androidpicker.qqtheme.framework.util.PickerUtil;

import java.io.IOException;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import de.greenrobot.event.EventBus;
import de.greenrobot.event.Subscribe;
import de.greenrobot.event.ThreadMode;
import me.jessyan.autosize.internal.CustomAdapt;

/**
 * Created by leung on 2018/11/1.
 */

public class EquipmentActivationActivity extends BaseActivity implements NavigationBar.NavigationBarListener, EquipmentContract.View, CustomAdapt {


    @BindView(R.id.qrCode)
    EditText qrCode;
    @BindView(R.id.address)
    LinearLayout address;
    @BindView(R.id.addresstxt)
    TextView addresstxt;
    @BindView(R.id.addressicon)
    TextView addressicon;
    @BindView(R.id.detailedAddress)
    EditText detailedAddress;
    EquipmentPresenter presenter;
    @BindView(R.id.sureactivation)
    Button sureactivation;


    public static void start(Activity activity) {
        Intent intent = new Intent();
        intent.setClass(activity, EquipmentActivationActivity.class);
        activity.startActivity(intent);
    }

    @BindView(R.id.navigationBar)
    NavigationBar navigationBar;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_equipment_activation);
        ButterKnife.bind(this);
        EventBus.getDefault().register(this);
        navigationBar.setNavigationBarListener(this);
        presenter = new EquipmentPresenter(this);

        detailedAddress.addTextChangedListener(new SimpleTextWatcher() {
            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                presenter.setdetAddress(s.toString());
            }
        });
        qrCode.addTextChangedListener(new SimpleTextWatcher() {
            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                presenter.setdeviceNo(s + "");
            }
        });
    }

    @Override
    public boolean isBaseOnWidth() {
        return true;
    }

    @Override
    public float getSizeInDp() {
        return 360;
    }

    public abstract static class SimpleTextWatcher implements TextWatcher {

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void afterTextChanged(Editable s) {

        }
    }

    @Override
    public void navigationLeft() {
        finish();
    }

    @Override
    public void navigationRight() {

    }

    @OnClick({R.id.scan, R.id.address, R.id.sureactivation})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.scan:
                IntentIntegrator intentIntegrator = new IntentIntegrator(this);
                intentIntegrator.setCaptureActivity(ScanActivity.class);
                intentIntegrator.setPrompt("请将二维码置于取景框内扫描");
                intentIntegrator.setOrientationLocked(true);
                intentIntegrator.initiateScan();
                break;
            case R.id.address:
                String json = null;
                try {
                    json = ConvertUtils.toString(getAssets().open("city.json"));
                } catch (IOException e) {
                    e.printStackTrace();
                }

                PickerUtil.onAddressPicker(this, json, new AddressPickTask.Callback() {
                    @Override
                    public void onAddressInitFailed() {
                        Toast.makeText(EquipmentActivationActivity.this, "获取地址列表失败！", Toast.LENGTH_LONG).show();
                    }

                    @Override
                    public void onAddressPicked(Province province, City city, County county) {
                        String provinces = province.getAreaName();
                        String citys = city.getAreaName();
                        String countys = county.getAreaName();
                        presenter.setprovCode(province.getAreaId());
                        presenter.setcityCode(city.getAreaId());
                        presenter.setareaCode(county.getAreaId());
                        showAddress(provinces + " " + citys + " " + countys);
                        Toast.makeText(EquipmentActivationActivity.this, provinces + citys + countys, Toast.LENGTH_LONG).show();
                    }
                });
                break;
            case R.id.sureactivation:
                if (detailedAddress.getText().toString().equals("")) {
                    Toast.makeText(this, "请输入地址！", Toast.LENGTH_LONG).show();
                    return;
                }
                if (qrCode.getText().toString().equals("")) {
                    Toast.makeText(this, "请输入设备编号或者扫描设备上的二维码！", Toast.LENGTH_LONG).show();
                } else if (addresstxt.getText().toString().equals("")) {
                    Toast.makeText(this, "请选择省市区！", Toast.LENGTH_LONG).show();
                } else {
                    //跳转查询支付
                    presenter.devicOrderPay(qrCode.getText().toString());
                }

                break;
        }
    }

    @Subscribe(threadMode = ThreadMode.MainThread)
    public void payStateActive(PayState paystate) {

        if (paystate.isSucess()) {
            presenter.activation();
        } else {
            Toast.makeText(getApplicationContext(),"押金支付失败,请重新支付",Toast.LENGTH_LONG).show();
            this.dismissLoading();
            //失败
        }
    }

    // Get the results:
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        IntentResult result = IntentIntegrator.parseActivityResult(requestCode, resultCode, data);
        if (result != null) {
            if (result.getContents() == null) {
                Toast.makeText(this, "取消扫码", Toast.LENGTH_LONG).show();
            } else {
                showQRcode(result.getContents());
                Toast.makeText(this, " " + result.getContents(), Toast.LENGTH_LONG).show();
            }
        } else {
            super.onActivityResult(requestCode, resultCode, data);
        }
    }


    @Override
    public void showQRcode(String s) {
        presenter.setdeviceNo(s);
        qrCode.setText(s);
    }

    @Override
    public void showAddress(String s) {
        // presenter.setdetAddress(s);
        addresstxt.setText(s);
        enshowadressicon();
        Toast.makeText(this, s, Toast.LENGTH_LONG).show();
    }

    @Override
    public void enshowadressicon() {
        addressicon.setVisibility(View.GONE);
    }

    @Override
    public void commitSuccess() {
        ActivationSuccessActivity.start(this);
        finish();

    }

    @Override
    public void commitFail(String s) {
        Toast.makeText(this, s, Toast.LENGTH_LONG).show();
    }

    @Override
    public void showCommit() {
        sureactivation.setEnabled(true);
        sureactivation.setBackground(getResources().getDrawable(R.drawable.background_btn04));
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    @Override
    public void enshowCommit() {
        sureactivation.setEnabled(false);
        sureactivation.setBackground(getResources().getDrawable(R.drawable.background_btn_grey04));
    }

    @Override
    public Activity getCurruntActivity() {
        return this;
    }


}
